package me.zhengjie.pm.projectdepartment.service.impl;

import me.zhengjie.pm.projectdepartment.domain.ProjectDepartment;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.pm.projectdepartment.repository.ProjectDepartmentRepository;
import me.zhengjie.pm.projectdepartment.service.ProjectDepartmentService;
import me.zhengjie.pm.projectdepartment.service.dto.ProjectDepartmentDto;
import me.zhengjie.pm.projectdepartment.service.dto.ProjectDepartmentQueryCriteria;
import me.zhengjie.pm.projectdepartment.service.mapper.ProjectDepartmentMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZGZ
* @date 2020-04-14
*/
@Service
//@CacheConfig(cacheNames = "projectDepartment")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectDepartmentServiceImpl implements ProjectDepartmentService {

    private final ProjectDepartmentRepository projectDepartmentRepository;

    private final ProjectDepartmentMapper projectDepartmentMapper;

    public ProjectDepartmentServiceImpl(ProjectDepartmentRepository projectDepartmentRepository, ProjectDepartmentMapper projectDepartmentMapper) {
        this.projectDepartmentRepository = projectDepartmentRepository;
        this.projectDepartmentMapper = projectDepartmentMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ProjectDepartmentQueryCriteria criteria, Pageable pageable){
        Page<ProjectDepartment> page = projectDepartmentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(projectDepartmentMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ProjectDepartmentDto> queryAll(ProjectDepartmentQueryCriteria criteria){
        return projectDepartmentMapper.toDto(projectDepartmentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ProjectDepartmentDto findById(Integer id) {
        ProjectDepartment projectDepartment = projectDepartmentRepository.findById(id).orElseGet(ProjectDepartment::new);
        ValidationUtil.isNull(projectDepartment.getId(),"ProjectDepartment","id",id);
        return projectDepartmentMapper.toDto(projectDepartment);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ProjectDepartmentDto create(ProjectDepartment resources) {
        return projectDepartmentMapper.toDto(projectDepartmentRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectDepartment resources) {
        ProjectDepartment projectDepartment = projectDepartmentRepository.findById(resources.getId()).orElseGet(ProjectDepartment::new);
        ValidationUtil.isNull( projectDepartment.getId(),"ProjectDepartment","id",resources.getId());
        projectDepartment.copy(resources);
        projectDepartmentRepository.save(projectDepartment);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            projectDepartmentRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProjectDepartmentDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectDepartmentDto projectDepartment : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("项目", projectDepartment.getProjectId());
            map.put("部门", projectDepartment.getDepartmentId());
            map.put("部门承担责任", projectDepartment.getType());
            map.put("描述", projectDepartment.getDescribe());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}